import subprocess as sp
from flask import Flask, send_from_directory, render_template, request


class HlsVideoFeed():
    def __init__(self,name):
        pass
        #sp.run(['python3', '/home/rancher/python/code/hls.py', name])
        """
        rtmpUrl = "rtsp://36.133.66.40:8554/"+name
        hlSfile = '/home/rancher/python/data/'+name+'.m3u8'

        command = ['ffmpeg',
                   '-i',
                   rtmpUrl,
                   '-f', 'rawvideo',
                   '-vcodec', 'rawvideo',
                   '-pix_fmt', 'bgr24',
                   '-c:v', 'libx264',
                   '-c:a', 'aac',
                   '-strict', str(-2),
                   '-f', 'hls',
                   hlSfile
                   ]
        p = sp.Popen(command, stdin=sp.PIPE)
        """

    def get(self, file_name):
        video_dir = '/home/rancher/python/data/'
        return send_from_directory(video_dir, file_name+".m3u8", as_attachment=True)


app = Flask(__name__)


@app.route('/')
def index():
    # jinja2模板，具体格式保存在index.html文件中
    return render_template('index.html')


@app.route('/video_feed')
def download():
    filename = request.args.get("name","")
    feed = HlsVideoFeed(filename);
    print(filename)
    return feed.get(filename);


if __name__ == '__main__':
    app.run()
